package com.shirotest.shiro_test.cofig;

import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @Author: 秃头不用洗发水
 * @Date: 2021-03-21
 * @Description:
 */
@Configuration
public class ShiroConfig {
    @Bean(name = "Realm")
    public ShiroRealm shiroRealm(){
        return new ShiroRealm();
    }

    @Bean(name = "SecurityManager")
    public DefaultWebSecurityManager defaultWebSecurityManager(@Qualifier("Realm") ShiroRealm realm){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(realm);
        return securityManager;
    }
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(@Qualifier("SecurityManager")DefaultWebSecurityManager securityManager){
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        bean.setSecurityManager(securityManager);
        //过滤器
        Map<String,String>  filterMap = new LinkedHashMap<>();
        filterMap.put("/user/add","authc");
        bean.setFilterChainDefinitionMap(filterMap);
        //设置登录请求页面
        bean.setLoginUrl("/login");
        return bean;
    }
}
